package com.itheima.health.controller.backend;

import com.itheima.health.constant.RedisConstant;
import com.itheima.health.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.Set;

/**
 * 2024/7/8-12:12
 * 功能:
 */
@Configuration
@Slf4j
public class ScheduledTask {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 每一分钟执行一次删除垃圾图片
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void deletePicScheduled() {
        SetOperations ops = redisTemplate.opsForSet();
        Set difference = ops.difference(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (difference != null) {
            log.info("垃圾图片数量：{}", difference.size());
            for (Object pic : difference) {
                String url = "http://sga8l5xzi.hn-bkt.clouddn.com/" + pic;
                log.info("删除垃圾图片：{}", pic);
                QiniuUtils.deleteFileFromQiniu((String) pic);
                redisTemplate.delete(pic);
            }
        }

    }

}
